package com.hcrazy.manchester.app.controllers.radio;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hcrazy.manchester.app.pojo.ErrorMsg;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.services.RadioChannelService;
import com.hcrazy.manchester.app.services.RadioProgramService;
import com.hcrazy.manchester.app.services.RadioTagService;
import com.hcrazy.manchester.app.utils.Constant;
import com.hcrazy.manchester.app.utils.Log;

@Controller
@RequestMapping("/api/radio")
public class RadioAPIController {
	
	@Autowired
	private RadioTagService radioTagService;
	
	@Autowired
	private RadioChannelService radioChannelService;
	
	@Autowired
	private RadioProgramService programService;

	@RequestMapping(value="/types",method=RequestMethod.GET)
	public @ResponseBody Object getTypes(){
		try {
			return radioTagService.getByParentId(Constant.RADIO_ID_CTYPE);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	
	}
	
	@RequestMapping(value="/styles",method=RequestMethod.GET)
	public @ResponseBody Object getStyles(){
		try {
			return radioTagService.getByParentId(Constant.RADIO_ID_CSTYLE);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	
	}
	
	@RequestMapping(value="/scenes",method=RequestMethod.GET)
	public @ResponseBody Object getScenes(){
		try {
			return radioTagService.getByParentId(Constant.RADIO_ID_CSCENE);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	
	}
	
	@RequestMapping(value="/channels",method=RequestMethod.GET)
	public @ResponseBody Object getChannels(
			@RequestParam(required=false) Integer cType,
			@RequestParam(required=false) Integer cStyle,
			@RequestParam(required=false) Integer cScene,
			@RequestParam(required=false) Integer pageNum,
			@RequestParam(required=false) Integer numPerPage,
			@RequestParam(required=false) String search){
		try {
			if(cType == null)
				cType = 0;
			if(cStyle == null)
				cStyle = 0;
			if(cScene == null)
				cScene = 0;
			if(numPerPage == null)
				numPerPage = 8;
			if(pageNum == null)
				pageNum = 1;
			
			if(numPerPage != null && numPerPage == 0){
				return radioChannelService.getAllInfosBySortId(cType, cScene, cStyle, search);
			}
			
			PageModel model = new PageModel();
			model.setPageNum(pageNum);
			model.setNumPerPage(numPerPage);
			
			return radioChannelService.getAddressInfos(model, search, cType, cScene, cStyle);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	
	}
	
	@RequestMapping(value="/channel",method=RequestMethod.GET)
	public @ResponseBody Object getChannelById(@RequestParam int channelId){
		try {
			return radioChannelService.getInfoById(channelId);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
			
	@RequestMapping(value="/programs",method=RequestMethod.GET)
	public @ResponseBody Object getPrograms(
			@RequestParam(required=false) Integer channelId,
			@RequestParam(required=false) Integer pageNum,
			@RequestParam(required=false) Integer numPerPage,
			@RequestParam(required=false) String search){
		
		try {
			if(null == channelId)
				channelId = 0;
			if(null == pageNum)
				pageNum = 1;
			if(null == numPerPage)
				numPerPage = 8;
			
			if(null != numPerPage && numPerPage == 0){
				return programService.getAllInfosByChannelId(channelId, false);
			}
			
			PageModel model = new PageModel();
			model.setNumPerPage(numPerPage);
			model.setPageNum(pageNum);
			
			return programService.getPagedInfos(model, search, channelId, 0);
		}
		catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
	
	@RequestMapping(value="/program",method=RequestMethod.GET)
	public @ResponseBody Object getProgramById(@RequestParam int programId){
		try {
			return programService.getById(programId);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return new ErrorMsg(Constant.ERROR_CODE_SYSTEM_EXCEPTION,e.getMessage());
		}
	}
}
